package com.flot.yazb.home.moudle.message.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.flot.yazb.R;
import com.flot.yazb.base.BApplication;
import com.flot.yazb.model.nearly.TranslatingModel;
import com.flot.yazb.widget.utils.GlideImageLoaderUtil;
import com.flot.yazb.widget.utils.TimeUtils;
import com.github.library.bubbleview.BubbleTextVew;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;


public class ChatMessageAdapter extends BaseListAdapter<TranslatingModel> {

    private Context mContext;
    private String thump;

    public static final int TYPE_LEFT = 0;
    public static final int TYPE_RIGHT = 1;

    public ChatMessageAdapter(Context context, List<TranslatingModel> list, String ump) {
        super(context, list);
        mContext = context;
        thump = ump;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (getItem(position).type == TYPE_LEFT) {
            return TYPE_LEFT;
        }
        return TYPE_RIGHT;
    }

    private View createViewByType(int position) {
        if (getItem(position).type == TYPE_LEFT) {
            return mInflater.inflate(R.layout.item_conversation_left, null);
        }
        return mInflater.inflate(R.layout.item_conversation_right, null);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = createViewByType(position);
        }

        final TranslatingModel entity = getItem(position);
        CircleImageView icon = ViewHolder.get(convertView, R.id.civ_avatar);
        TextView tvTime = ViewHolder.get(convertView, R.id.tv_time);
        BubbleTextVew btvMessage = ViewHolder.get(convertView, R.id.btv_message);
        ImageView imv = (ImageView) convertView.findViewById(R.id.ivimg);
        btvMessage.setText(entity.content);
        if (getItem(position).type == TYPE_LEFT){
            GlideImageLoaderUtil.loadImage(icon, thump, R.mipmap.activity_splash_background);
        }else{
            GlideImageLoaderUtil.loadImage(icon, BApplication.getInstance().getUserModel().avatar, R.mipmap.activity_splash_background);
        }

        if (entity.isShowPic == true) {
            if (entity.images != null && entity.images.size() > 0) {
                imv.setVisibility(View.VISIBLE);
                btvMessage.setVisibility(View.GONE);
                GlideImageLoaderUtil.loadImage(imv, entity.images.get(0));
            } else {
                btvMessage.setVisibility(View.VISIBLE);
                imv.setVisibility(View.GONE);
            }
        } else {
            imv.setVisibility(View.GONE);
            btvMessage.setVisibility(View.VISIBLE);
        }


        if (isDisplayTime(position)) {
            tvTime.setVisibility(View.VISIBLE);
            tvTime.setText(TimeUtils.formatTime(Long.valueOf(entity.time), true, mContext));
        } else {
            tvTime.setVisibility(View.GONE);
        }
        return convertView;
    }

    //  一分钟内的请求与回复不显示时间
    public boolean isDisplayTime(int position) {
        if (position > 0) {
            if ((Long.valueOf(getItem(position).time) - Long.valueOf(getItem(position - 1).time)) > (60 * 1000)) {
                return true;
            } else {
                return false;
            }
        } else if (position == 0) {
            return true;
        } else {
            return false;
        }
    }

}
